<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
      <el-form-item label="标签分类名称" prop="tagTypeName">
        <el-input
          v-model="queryParams.tagTypeName"
          placeholder="请输入标签分类名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="标签状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择标签状态" clearable>
          <el-option
            v-for="dict in dict.type.sys_tag_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:tagType:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:tagType:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:tagType:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:tagType:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="tagTypeList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="标签分类ID" align="center" prop="tagTypeId" />
      <el-table-column label="标签分类名称" align="center" prop="tagTypeName" >
        <template slot-scope="scope">
          <el-input v-model="scope.row.tagTypeName" @blur="upDataTypeName($event,scope.row)" />
        </template>

      </el-table-column>
      <el-table-column label="标签状态" align="center" prop="status">
        <template slot-scope="scope">
          <el-switch @change="elSwitchStatus($event,scope.row)"
            v-model="scope.row.status=='0'?true:false"
            active-color="#13ce66"
            inactive-color="#ccc">
          </el-switch>
<!--          <dict-tag :options="dict.type.sys_tag_status" :value="scope.row.status"/>-->
        </template>
      </el-table-column>
<!--      <el-table-column label="备注" align="center" prop="remark" />-->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleAddTagData(scope.row)"
            v-hasPermi="[['system:tagData:add']]"
          >标签数据</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:tagType:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改组织机构标签对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="50%" append-to-body center @close="closeDialogTypeData">
      <h2 style="padding-bottom: 10px;font-size: 16px;">标签类型：{{tagTypeRow.tagTypeName}}</h2>
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleAddTypeData"
            v-hasPermi="['system:tagData:add']"
          >新增</el-button>
        </el-col>
      </el-row>
      <el-table v-loading="loading" :data="tagDataList" @selection-change="handleSelectionChange">
<!--        <el-table-column type="selection" width="55" align="center" />-->
        <el-table-column label="标签ID" align="center" prop="tagId" />
<!--        <el-table-column label="标签类型Id" align="center" prop="tagTypeId" />-->
        <el-table-column label="标签值" align="center" prop="tagValue">
          <template slot-scope="scope">
            <el-input v-model="scope.row.tagValue" @blur="upDataTagValue($event,scope.row)" />
          </template>
        </el-table-column>
<el-table-column label="显示顺序" align="center" prop="sort" />
<!--        <el-table-column label="备注" align="center" prop="remark" />-->
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="tagDataDelete(scope.row)"
              v-hasPermi="['system:tagData:remove']"
            >删除</el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-top"
              @click="handleMoveUp(scope.row)"
              v-hasPermi="['system:tagData:remove']"
            >上移</el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-bottom"
              @click="handleMoveDown(scope.row)"
              v-hasPermi="['system:tagData:remove']"
            >下移</el-button>
          </template>
        </el-table-column>
      </el-table>


    </el-dialog>
  </div>
</template>

<script>
import { listTagType, getTagType, delTagType, addTagType, updateTagType } from "@/api/zzjggl/tagType";
import { listTagData,listTagDataByTagId, getTagData, delTagData, addTagData, updateTagData,moveUp,moveDown } from "@/api/zzjggl/tagData";

export default {
  name: "TagType",
  dicts: ['sys_tag_status'],
  data() {
    return {
      /**标签数据*/
      tagDataList: [],//标签数据
      tagTypeRow:{},//标签类型信息[点击的那一行]
      tagDataform:{},//标签数据参数
      sortNum:0,
      /**标签类型*/
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 组织机构标签表格数据
      tagTypeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        tagTypeName: null,
        status: null,
      },
      // 标签类型参数
      form: {},
      // 表单校验
      rules: {
        tagTypeName: [
          { required: true, message: '请输入活动名称', trigger: 'blur' },
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /**标签数据*/
    //弹出标签数据对话框
    handleAddTagData(row){
     this.tagDataList =[];
      this.tagTypeRow={...row}
      listTagDataByTagId(this.tagTypeRow.tagTypeId).then(res=>{
        this.tagDataList =[...res.data]
        //初始化排序
        let ls=this.tagDataList.map(item=> item.sort)//把标签数据数组的sort组成一个新的数组
        if(ls.length>0){
            this.sortNum=Math.max(...ls)//找出最大值
        }else{
          this.sortNum=0
        }


      })
       this.open = true;
      this.title = "组织机构标签数据";
    },

    //关闭弹窗的按钮
    closeDialogTypeData(e){

    },

    //添加标签数据
    handleAddTypeData(){
      this.resetTagData();
      this.sortNum++
      this.tagDataform.sort=this.sortNum
      this.tagDataform.tagTypeId=this.tagTypeRow.tagTypeId
      addTagData(JSON.stringify(this.tagDataform)).then(response => {
        this.$modal.msgSuccess("新增成功");
       this.getTagDataListById(this.tagDataform.tagTypeId)
      });
    },
    //修改标签数据
    upDataTagValue(e,row){
      this.resetTagData()
      this.tagDataform.tagId=row.tagId
      this.tagDataform.tagValue=row.tagValue
      console.log(this.tagDataform,row)
      updateTagData(JSON.stringify(this.tagDataform)).then(response => {
      if(response.code==200) this.$modal.msgSuccess("修改成功");

      });
    },
    //删除
    tagDataDelete(row) {
      const tagIds = row.tagId
      this.$modal.confirm('是否确认删除组织标签数据编号为"' + tagIds + '"的数据项？').then(function() {
        return delTagData(tagIds);
      }).then(() => {
        this.getTagDataListById(row.tagTypeId)
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },

    resetTagData() {
      this.tagDataform = {
        tagId: null,
        tagTypeId: null,
        tagValue: null,
        sort: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("tagDataform");
    },
    //通过标签类型查询标签数据
    getTagDataListById(tagTypeId){
      this.loading = true;
      return listTagDataByTagId(tagTypeId).then(res=>{
        this.tagDataList=[]
        this.tagDataList =[...res.data]
        this.loading = false;
      })
    },
    //查询组织标签数据列表【所有的标签】
    getTagDataList() {
      this.loading = true;
      listTagData({pageNum: 1,pageSize: 10}).then(response => {
        this.tagDataList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },

    //上移数据
    handleMoveUp(row){
      moveUp(row.tagId).then(res=>{
        this.getTagDataListById(row.tagTypeId)
      }).catch(err=>{
        console.log(err)
      })
    },
    //下移数据
    handleMoveDown(row){
      moveDown(row.tagId).then(res=>{
        this.getTagDataListById(row.tagTypeId)
      }).catch(err=>{
        console.log(err)
      })
    },

    /**标签类型*/
    //改变表格名称
    upDataTypeName(e,row){
      this.form.tagTypeId = row.tagTypeId
      this.form.tagTypeName = row.tagTypeName
      updateTagType(JSON.stringify(this.form)).then(response => {
        this.$modal.msgSuccess("修改成功");
        this.getList();
      });
    },
    //改变表格状态
    elSwitchStatus(e,row){
      console.log(e,row)

      if(e) {

            this.form.status = '0'
      }else{
            this.form.status = "1"
          }



      this.form.tagTypeId = row.tagTypeId
        updateTagType(JSON.stringify(this.form)).then(response => {
          this.$modal.msgSuccess("修改成功");
          this.getList();
        });

    },
    /** 查询组织机构标签列表 */
    getList() {
      this.loading = true;
      listTagType(this.queryParams).then(response => {
        this.tagTypeList = response.rows;
        this.total = response.total;
        this.loading = false;
        console.log(this.tagTypeList)
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        tagTypeId: null,
        tagTypeName: null,
        status: '0',
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.tagTypeId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.resetQuery()
      this.reset();
      addTagType(JSON.stringify(this.form)).then(response => {
        this.$modal.msgSuccess("新增成功");
        this.getList();
      });
     /* this.open = true;
      this.title = "添加组织机构标签";*/
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const tagTypeId = row.tagTypeId || this.ids
      getTagType(tagTypeId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改组织机构标签";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.tagTypeId != null) {
            updateTagType(JSON.stringify(this.form)).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addTagType(JSON.stringify(this.form)).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const tagTypeIds = row.tagTypeId || this.ids;
      console.log(tagTypeIds)
      this.$modal.confirm('是否确认删除组织机构标签编号为"' + tagTypeIds + '"的数据项？').then(function() {
        return delTagType(tagTypeIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/tag/type/export', {
        ...this.queryParams
      }, `tagType_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
<style lang="scss" scoped>
::v-deep{
  .el-checkbox__label{display: none}
  .el-dialog {
    border-radius: 11px;
  }
}
</style>
